﻿$.extend({
    Comment: {

        defaults: {
            url: '/Comment/?targetId={0}&p={1}',
            container: '#comment_list',
            feedbackId: '#feedback',
            replyId: '#replyId',
            placeholder: null,
            targetId: 0,
            pageIndex: 1,
            loading: true,
            scrollPaging: false
        },

        init: function (options) {

            var t;
            this.options = t = $.extend({}, this.defaults, options || {});
            
            if (t.scrollPaging) {

                $(window).bind('scroll', $.proxy(function () {

                    var v = $.viewport();
                    var c = $(t.container);

                    //alert(c.offset().top + ': '+ v.top);
                    //alert($(this.container).height());
                    //if (v.top >= $(this.container).offset().top + $(window).height()) {
                    //if (v.top >= c.offset().top + v.height && this.pageIndex != parseInt(c.attr('pageindex'))) {
                    if (v.top + v.height >= c.offset().top && t.pageIndex != parseInt(c.attr('pageindex'))) {

                        //alert(v.top);

                        this.next();

                        //c.attr('pageindex', this.pageIndex);

                        //alert(v.top);
                    }



                }, this));
            }



        },

        next: function () {

            this.options.pageIndex++;

            var t = this.options;

            if (t.loading && $.Loading) {

                $.Loading.show();
            }

            var c = $(t.container);

            c.load($.String.format(t.url, t.targetId, t.pageIndex), $.proxy(function (data) {

                c.append(data).attr('pageindex', t.pageIndex);

                if (t.loading && $.Loading) {
                    $.Loading.hide();
                }

            }, this));

        },

        show: function (_target) {

            if (_target)
                $(_target).hide();

            this.options.placeholder = _target;

            this.reply(0, _target);
        },

        reply: function (_Id, _targetId) {
            var _boxId = this.options.feedbackId;
            var _reply = this.options.replyId;
            var _placeholder = this.options.placeholder;
            var _target = _targetId;

            if (_target === undefined) {
                _target = '#reply_' + _Id;

            };


            $(_boxId).insertAfter(_target);
            //$(_target).attr('rel', 1);
            $(_boxId).fadeIn(500, function () {
                $(_reply).val(_Id);
                var _textbox = $(_boxId + ' textarea').focus().focusout(function () {
                    var _val = $.trim($(this).val()).replace(/\n/g, '');
                    if (_val === '') {
                        $(_boxId).hide();

                        if (_placeholder)
                            $(_placeholder).show();

                    }
                });

                if ($.fn.autogrow) {
                    _textbox.autogrow();
                };
            });

        },

        cancel: function () {
            var f = this.options.feedbackId;
            $(f + ' textarea').val('');
            $(f).fadeOut($.proxy(function () {

                if (this.options.placeholder)
                    $(this.options.placeholder).show();

            }, this));
        }

    }
});